/*
  * Classe de gestion pour les Tree
  */
function Tree(col, id)
{
	// Attributs
	this.ligne = new Array();
	this.nombreColonne = col.length;
	this.colonnes = col;
	this.rowCount = 0;
	this.viewId = id;
	this.path = "";

	// Methodes
	this.EncodePath = function(newPath) {
		newPath = newPath.replace(/\\/g, "/");
		newPath = escape(newPath);
		if(newPath.charAt(newPath.length-1) != '/') {
			newPath += '/';
		}
		newPath = newPath.replace('%3A', ':');
		return(newPath);
	}
	this.GetPath = function() {
		return(this.path);
	}
	this.SetPath = function(newPath) {
		this.path = unescape(newPath);
	}
	this.AddLigne = function(ln) {
		var newLigne = new Array(this.nombreColonne);

		for(var i=0; i<this.nombreColonne; i++) {
			newLigne[this.colonnes[i]] = ln[i];
		}
		this.ligne.push(newLigne);
		this.rowCount++;
	}
	this.GetLigne = function(id) {
		return(this.ligne[id]);
	}
	this.SetCell = function(ligneId, colId, value) {
		this.ligne[ligneId][this.colonnes[colId]] = value;
	}
	this.GetCell = function(ligneId, colId) {
		return(this.ligne[ligneId][this.colonnes[colId]]);
	}
	this.GetColName = function(id) {
		return(this.colonnes[id]);
	}
	this.GetLignes = function() {
		return(this.ligne);
	}
	this.RmLigne = function(id) {
		this.ligne.splice(id, 1);
		this.rowCount--;
	}
	this.Clear = 	function() {
		this.ligne.splice(0, this.ligne.length);
		this.rowCount = 0;
	}
	this.SetLigne = function(ln) {
		for(var i=0; i<this.nombreColonne; i++) {
			this.ligne[this.colonnes[i]] = ln[i];
		}
	}
	this.getCellText = function(row, column) {
		if(this.ligne[row] != null) {
			return(" " + this.ligne[row][column.id]);
		}
		return null;
	}
	this.setTree = function(treebox) {
		this.sort(this.colonnes[0], 'ascending'); // this.colonnes[0] -> la premiere colonne normalement le nom de fichiers/dossiers
		this.treebox = treebox;
	}
	this.isContainer = function(row) {
		return false;
	}
	this.isSeparator = function(row) {
		return false;
	}
	this.isSorted = function(row) {
		return false;
	}
	this.getLevel = function(row) {
		 return 0;
	}
	this.getImageSrc = function(row, column) {
		if(column.id == this.colonnes[0] && this.nombreColonne > 3) {
			if((this.ligne[row][this.colonnes[3]]).charAt(0) != 'd' && oInterface.os != 1) {
				return("moz-icon://file://" + this.path+this.ligne[row][this.colonnes[0]] + "?size=16");
			}
		}
		return(null);
	}
	this.getCellProperties = function(row, column, props) {
		if(column.id == this.colonnes[0] && this.nombreColonne > 3) {
			var aserv = Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);

			if((this.ligne[row][this.colonnes[3]]).charAt(0) == 'd') {
				props.AppendElement(aserv.getAtom("dossier"));
			} else {
				if(oInterface.os == 1) {
					props.AppendElement(aserv.getAtom("fichier"));
				}
			}
		}
	}
	this.cycleHeader = function(column) {
		var sortDirection = column.element.getAttribute("sortDirection");
		for(var i=0; i<this.nombreColonne; i++) {
			if(column.id == this.colonnes[i]) {
				if(sortDirection == "ascending") {
					column.element.setAttribute("sortDirection", "descending");
					this.sort(column.id, "descending");
				} else {
					column.element.setAttribute("sortDirection", "ascending");
					this.sort(column.id, "ascending");
				}
			} else {
				var otherCol = document.getElementById(this.colonnes[i]);
				otherCol.setAttribute("sortDirection", "natural");
			}
		}
	}
	this.sort = function(column, sens) {
		if (this.nombreColonne == 4)
		{
			//dans le cas des trees de la fenetre principale.
			if(this.rowCount > 0 && column == this.colonnes[0]) { // this.colonnes[0] -> la premiere colonne normalement le nom de fichiers/dossiers
				var debut = 0;
				if(this.ligne[0][this.colonnes[0]] == '..') {
					debut = 1;
				}
				this.tri_quicksort(this.ligne, this.colonnes, debut, this.ligne.length-1, column, sens);
			}
		}
		return false;
	}
	this.getRowProperties = function(row, props) {}
	this.getColumnProperties = function(colid, column, props) {}
	
	this.tri_quicksort = function(tab, col, inf, sup, colName, sens) {
		var place = inf;
		var j = sup;
		var tmp, compResult;

		if(inf < sup) {
			while(place < j) {
				if(this.compar(tab, col, colName, place, j, sens)) {
					j--;
				} else {
					tmp = tab[place];
					tab[place] = tab[j];
					tab[j] = tmp;

					place++;

					tmp = tab[place];
					tab[place] = tab[j];
					tab[j] = tmp;
				}
			}
			this.tri_quicksort(tab, col, inf, place-1, colName, sens);
			this.tri_quicksort(tab, col, place+1, sup, colName, sens);
		}
	}
	this.compar = function(ligne, col, colName, i, j, sens) {
		if(colName == col[0]) { //comparaison sur les nom de dossier
			if(ligne[i][col[3]].charAt(0) == 'd' && ligne[j][col[3]].charAt(0) != 'd') {
				return(true);
			} else if(ligne[i][col[3]].charAt(0) != 'd' && ligne[j][col[3]].charAt(0) == 'd') {
				return(false);
			} else {
				var retour = false;
				if(ligne[i][colName] < ligne[j][colName]) {
					retour = true;
				}
				if(sens == 'ascending') {
					return(retour);
				} else {
					return(!retour);
				}
			}
		}
		return(true);
	}
}

